<?php
// 本类由系统自动生成，仅供测试用途
class AccountAction extends Action {
	private $_uid = '';
	public function _initialize(){
		$this->_uid = session('uid');
		$data = array(
			''	
		);
		if(!$this->_uid) $this->redirect("Public/login");//(503,"对不起，您登录超时了，请重新登录！",1);
		//B('Authenticate', $action);
	}
    public function index(){
		$Assets = M('Assets');
		$as = $Assets->where('uid = 8')->find();
		dump($as);
		$this->display();
    }
	
	/*======================================================================*\
		Function:	userProfile
		Purpose:	个人信息查看与修改
		Input:		$params			        参数
		Output:		模板输出		    	#
	\*======================================================================*/
	public function userProfile(){
		$UserProfile = M('UserProfile');
		$UserDepartment = M('UserDepartment');
		$post = $this->_param();
		$uid = $this->_uid;
		$model = M();
		if(trim($post['name']) == ''){
			$sql = "SELECT A.*,B.department_name,C.* FROM oa_user_department A
					LEFT JOIN oa_department B ON A.department_id = B.department_id
					LEFT JOIN oa_user_profile C ON C.owner_uid = A.owner_uid
					WHERE A.owner_uid = $uid LIMIT 1";
			//$data = $UserProfile->alias('A')->join('oa_department B on A.department_id = B.department_id')->field('A.*,B.department_name')->where("owner_uid = $uid")->find();
			$data = $model->query($sql);
			//dump($data);
			//echo $sql;
			$result = array(
				'uid' => $uid,
				'name' => $data[0]['name'],
				'department_name' => $data[0]['department_name'],
				'mobile' => $data[0]['mobile'],
				'address' => $data[0]['address'],
				'gender' => $data[0]['gender'],
				'email' => $data[0]['email'],
				'remark' => $data[0]['remark'],
			);
			if($data){
				$this->ajaxReturn($result,'个人资料',1);
			}else{
				$this->ajaxReturn(0,'error',0);
			}
			//$this->assign('list',$result);    
			//$this->display();
		}else{
			if($uid == 1){
				$uid = $_GET['uid'] ?  $_GET['uid'] : 1;//如果是admin登录更改员工信息时，更改的uid为员工uid
				if($UserDepartment->where("owner_uid = $uid")->select()){
					$departmentdata = array(
						'department_id' => $post['department_id'],
					);
					$depart = $UserDepartment->where("owner_uid = $uid")->save($departmentdata);
				}else{
					$departmentdata = array(
						'department_id' => $post['department_id'],
						'owner_uid' => $uid,
						'status' => 1
					);
					$depart = $UserDepartment->where("owner_uid = $uid")->add($departmentdata);
				}
				//if(!$depart) $this->ajaxReturn(0,'员工调动出错',0);
			}
			$result = $UserProfile->where("owner_uid = $uid")->find();
			$data = array(
				'owner_uid' => $uid,
				'name' => $post['name'],
				'mobile' => $post['mobile'],
				'address' => $post['address'],
				'gender' => $post['gender'],
				'email' => $post['email'],
				'zip_code' => $post['zip_code'],
				'remark'	=> $post['remark'],
				'update_time' => datetime(),
			);
			//if($this->_uid != 1) array_pop($data);//判断提交更改资料用户，如果是admin，则可以更改部门 
			//dump($data);
			if($result){
				if($UserProfile->where("owner_uid = $uid")->save($data)){
					logs($uid,'账号信息修改保存成功！',1);
					$this->ajaxReturn(1,"保存成功！",1);
				}else{
					logs($uid,'账号信息修改保存失败！',0);
					$this->ajaxReturn(0,"保存失败！",0);
				}

			}else{
				if($UserProfile->add($data)){
					logs($uid,'账号信息修改保存成功！',1);
					$this->ajaxReturn(1,"保存成功！",1);
				}else{
					logs($uid,'账号信息修改保存失败！',0);
					$this->ajaxReturn(0,"保存失败1！",0);
				}
			}
			
			
		}	
	}
	/*======================================================================*\
		Function:	userManage
		Purpose:	用户管理(人事)
		Input:		$params			        参数
		Output:		模板输出		    	#
	\*======================================================================*/
	public function userManage(){
		$Account = M('User');
		$model = M();
		$Department = M('Department');
		import('ORG.Util.Page');// 导入分页类
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		//$param = $this->_param();
		//$param['account'] ? $where['account'] = $param['account']:1;
		$sql = "SELECT D.uid,D.account,A.name,A.mobile,B.department_name FROM oa_user_department C
				LEFT JOIN oa_department B ON B.department_id = C.department_id
				RIGHT JOIN oa_user_profile A ON C.owner_uid = A.owner_uid
				RIGHT JOIN oa_user D ON D.uid = A.owner_uid
				WHERE D.is_deleted = 1";
		$count = $Account->count();// 查询满足要求的总记录数
		$Page = new Page($count,7);// 实例化分页类 传入总记录数和每页显示的记录数
		$show = $Page->show();// 分页显示输出
		$sql .= " LIMIT ".$Page->firstRow.",".$Page->listRows;
		$data = $model->query($sql);
		
		//echo $sql;
		//$data = $Account->alias('A')->join('oa_user_profile B on A.uid = B.owner_uid')->field('A.uid,A.account,B.name,B.mobile,B.update_time,B.department_id')->order('B.update_time DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
		//echo $Account->getLastSql();//打印sql语句
		//dump($data);
		$departments = $Department->select();//所有部门
		$this->assign('list',$data);// 赋值数据集
		$this->assign('department',$departments);
		//dump($list);
		$this->assign('page',$show);// 赋值分页输出
		$this->display();
	}
	public function checkAccount(){
		$GET = $this->_param();
		if($GET['account']){
			$Account = M('User');
			
			$data['account'] = trim($GET['account']);
			$is = $Account->where($data)->find();
			if($is){
				logs($uid,'注册失败：该账号已经注册',0,$GET['account']);
				$this->ajaxReturn(0,"该账号已经注册",0);

			}else{
				logs($uid,'注册失败：该账号可以注册',1,$GET['account']);
				$this->ajaxReturn(1,"该账号可以注册",1);
			}
		}
	}
	//修改密码
	public function resetPassword(){
		$get = $this->_get();
		$uid = session('uid');
		if(!$uid) $this->redirect('Public:login');
		$User = M('User');
		if($get['password']==''){
			$this->error("请输入密码");
		}else if($get['password'] != $get['repassword']){
			logs($uid,'修改密码：两次输入的密码不对',0);
			$this->error("两次输入的密码不对!");			
		}else{
			
			$data = array(
					'uid'      => $uid,
					'password' => $get['password']
			);
			if($User->save($data)) logs($uid,'修改密码：成功',1);
			else logs($uid,'修改密码：失败',0);
		}
	}
	/*******************************************************
	*function:员工信息表格
	*说明:用于前台调用（load）员工信息
	*******************************************************/
	public function userMsgInfo(){
		$UserProfile = M('UserProfile');
		$Department = M('Department');
		$model = M();
		$post = $this->_param();
		$uid = $post['uid'];
		$sql = "SELECT A.name,A.mobile,A.gender,A.email,B.department_id FROM oa_user_department C
				LEFT JOIN oa_department B ON B.department_id = C.department_id
				RIGHT JOIN oa_user_profile A ON C.owner_uid = A.owner_uid
				WHERE A.owner_uid = $uid LIMIT 1";
		$data = $model->query($sql);
		//$data = $UserProfile->alias('A')->join('oa_department B on A.department_id = B.department_id')->field('A.*,B.department_name')->where("owner_uid = $uid")->find();//员工信息
		$list = $Department->select();//所有部门
		$this->assign('userInfo',$data[0]);
		$this->assign("list",$list);
		$this->display();
	}
	/*******************************************************
	*function:员工管理界面中的部门检索
	*
	*******************************************************/
	public function searchByDepartment(){
		$UserDepartment = M('UserDepartment');
		$param = $this->_param();
		$department_id = $param['department_id'];
		$v = $UserDepartment->alias('A')
							->join('RIGHT JOIN oa_user B ON A.owner_uid = B.uid')
							->join('oa_user_profile C ON C.owner_uid = B.uid')
							->join('oa_department D ON A.department_id = D.department_id')
							->field('A.department_id,D.department_name,B.account,B.uid,C.name,C.mobile')->select();
		//$sql = $UserDepartment->getLastSql();
		//echo $sql;
		//dump($v);
		if($department_id == "-1") $department_id = NULL;
		$list = $this->returnSearchResult($department_id,$v);
		//dump($list);
		$this->assign("list",$list);
		$this->display('userManage');
	}
	/*******************************************************
	*function:根据不同的参数，返回相应的信息，即部门中的员工信息
	*参数: $id 部门的department_id
	*	   $list 所有员工信息的结果集
	*******************************************************/
	private function returnSearchResult($id,$list){
		$i = 0;
		foreach($list as $key=>$value){
			if($value["department_id"] == "-1" && $id == NULL){
				$result[$i] = $value;
				$i++;
				continue;
			}
			if($value["department_id"] == $id){
				$result[$i] = $value;
				$i++;
			}
		}
		return $result;
	}
	/*******************************************************
	*function:增加员工(该操作只能是指定用户)
	*参数:无 
	*	   
	*******************************************************/
	public function addWorker(){
		$Department = M('Department');
		$uid = $this->_uid;
		if($uid != 1) return;
		$departments = $Department->select();//所有部门
		$this->assign('list',$departments);
		$this->display();
	}
	/*******************************************************
	*function:辞退员工(该操作只能是指定用户)
	*参数:无 
	*	   
	*******************************************************/
	function workerResign(){
		$User = M('User');
		$UserDepartment = M('UserDepartment');
		$UserProfile = M('UserProfile');
		$post = $this->_param();
		if($post['owner_uid']){
			$uid = $post["owner_uid"];
			$data["is_deleted"] = 0;
			$Udata["status"] = 0;
			$removeUser = $User->where("uid = $uid")->save($data);
			if($removeUser){
				$removeUserProfile = $UserProfile->where("owner_uid = $uid")->save($data);
				if($removeUserProfile){
					$removeUserDepartment = $UserDepartment->where("owner_uid = $uid")->save($Udata);
					if($removeUserDepartment){
						$this->ajaxReturn(1,'操作成功',1);
					}else{
						$this->ajaxReturn(0,'系统出错:UserDepartment，请联系管理员！',0);
					}
				}else{
					$this->ajaxReturn(0,'系统出错:UserProfile，请联系管理员！',0);
				}
			}else{
				$this->ajaxReturn(0,'系统出错:User，请联系管理员！',0);
			}
		}else{
			$this->ajaxReturn(0,'系统出错，请联系管理员！',0);
		}
	}
}
